import React, { memo, useEffect, useState } from "react";
import { View, ScrollView } from '@tarojs/components'
import { Cell, Empty, Image } from '@antmjs/vantui'
import { formatDate } from "@/utils/fomatter";
import { queryNewList } from "@/services"
import Taro, { usePullDownRefresh } from '@tarojs/taro'
import './index.scss'


function HotNews() {

  const [news, setNews] = useState([])
  const getNewList = (flag?: boolean) => {
    queryNewList().then(res => {
      if (res.data) {
        let arr1 = res.data.filter(item => {
          return item.publishTime && item.publishTime <= new Date().getTime()
        })
        let arr2 = arr1.sort((a, b) => {
          return b.publishTime - a.publishTime
        })
        setNews(arr2)
        flag && setTimeout(() => { Taro.stopPullDownRefresh() }, 1000)
      }
    })
  }
  useEffect(() => {
    getNewList()

  }, [])
  usePullDownRefresh(async () => {
    getNewList(true)


  })
  return (
    <View className='news-body'>
      {
        news.length ?
          <ScrollView className='scroll-body' scroll-y='true'>
            {
              news.map((data, index) => {
                return (
                  <Cell
                    title={data?.newsTitle}
                    label={`${formatDate(data?.created, null, 'MM-DD')} | ${data.source}`}
                    titleStyle={{ fontWeight: 600, fontSize: '16px' }}
                    renderExtra={
                      data?.images ?
                        <Image width='74px' height='56px' src={data?.images} />
                        :
                        null

                    }
                    key={data?.newsId}
                    border={index == news.length - 1 ? false : true}
                    linkType='navigateTo'
                    url={'/homeDetail/hotNewsDetail/index?newsId=' + `${data?.newsId}`}
                  />

                )
              })
            }

          </ScrollView>
          :
          <Empty
            image='https://static.hzddyy.com/taiyi/tt/public_placeholder_nodata.png'
            description='暂无数据'
          />

      }


    </View>


  )
}

export default memo(HotNews)
